package com.game.config.mongo;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.convert.DbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;

import com.game.config.prop.FuncMongoConfigProperties;
import com.game.mongo.MongoDBAbstractFactory;
import com.mongodb.connection.ConnectionPoolSettings;
/**
 * 
 * @author ZGame
 * 功能库 funcMongoTemplate
 *
 */
@Configuration
public class FuncMongoTemplateConfig extends MongoDBAbstractFactory{


	@Autowired
	private FuncMongoConfigProperties fucMongoConfigProperties;

	@Override
	@Bean(name = "funcMongoTemplate")
	public MongoTemplate getMongoTemplate() throws Exception {
		MongoDatabaseFactory mongoDatabaseFactory = mongoDbFactory(fucMongoConfigProperties);
		DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDatabaseFactory);
		MongoMappingContext mappingContext = new MongoMappingContext();
		MappingMongoConverter mappingMongoConverter = new MappingMongoConverter(dbRefResolver, mappingContext);
		// mappingMongoConverter.setCustomConversions(conversions);
		mappingMongoConverter.setTypeMapper(new DefaultMongoTypeMapper(null));//去除class信息
		return new MongoTemplate(mongoDatabaseFactory, mappingMongoConverter);
	}

	@Override
	public ConnectionPoolSettings getConnectionPoolSettings() {
		ConnectionPoolSettings setting = ConnectionPoolSettings.builder()
				.maxWaitTime(fucMongoConfigProperties.getMaxWaitTime(), TimeUnit.MILLISECONDS).build();
		return setting;
	}

}
